﻿package character
{
	import gb.GameObject;
	import flash.events.*;
	public class Character extends GameObject
	{
		public static const DIR_LEFT = 1;
		public static const DIR_RIGHT = -1;

		protected static const STATE_FREE = 1;
		protected static const STATE_HURT = 2;
		protected static const STATE_ATTACK = 3;

		protected var FRAME_IDLE:int = 1;
		protected var FRAME_RUN:int = 2;
		protected var FRAME_JUMP:int = 3;
		protected var FRAME_LAND:int = 4;
		protected var FRAME_HURT:int = 5;
		protected var FRAME_DIE:int = 6;

		protected var nWaitingFrame:int = -1;

		protected var currentState:int;
		protected var attackMoves:Array;
		protected var currentMove:AttackMove;

		public var MAX_HP:int;
		public var MAX_SPEED:Number;
		protected var hp:int;
		//protected var curentFrame

		protected var speed:Number;
		
		public function getSpeed():Number{
			return speed;
		}

		function onFrameLoop(evt:Event):void
		{
			UpdateObject();
		}
		public function Character()
		{
			// constructor code
			gotoAndStop(FRAME_IDLE);
			currentState = STATE_FREE;
			scaleX = 0.8;
			scaleY = 0.8;
			addEventListener(Event.ENTER_FRAME,onFrameLoop);
		}
		public function Init(sX:Number,sY:Number,sPicth:Number,sUpV:Number)
		{
			currentState = STATE_FREE;
			if (sUpV > 0)
			{
				Jump(sUpV);
			}
			else
			{
				physicalCore.velocityUp = sUpV;
				Idle();
			}
			physicalCore.velocityX = 0;
			SetPosition(sX,sY);
			SetPicth(sPicth);

			hp = MAX_HP;
			
			this.x = sX;
		}

		override protected function FloorContact()
		{

			if (currentFrame == FRAME_JUMP || currentFrame == FRAME_LAND)
			{
				Idle();
				//gotoAndStop(FRAME_IDLE);
			}
		}
		override protected function Update()
		{
			if (nWaitingFrame == 0)
			{
				Idle();
			}
			nWaitingFrame--;
			if (currentState == STATE_ATTACK)
			{

			}
		}
		private function Idle()
		{
			/*if(currentState == STATE_HURT)
			{
			return;
			}*/
			currentState = STATE_FREE;
			if (physicalCore.IsOverground())
			{
				if (currentFrame != FRAME_IDLE)
				{
					gotoAndStop(FRAME_IDLE);
				}
			}
			else
			{
				if (currentFrame != FRAME_LAND)
				{
					gotoAndStop(FRAME_LAND);
				}
			}
		}
		public function Stop()
		{
			if (currentFrame == FRAME_RUN)
			{
				gotoAndStop(FRAME_IDLE);
			}
		}
		public function UseAttackMove(m:int):AttackMove
		{
			if (currentState == STATE_FREE)
			{
				currentState = STATE_ATTACK;
				var currentMove:AttackMove = attackMoves[m];
				nWaitingFrame = currentMove.nFrame;
				gotoAndStop(currentMove.frameIndex);
				currentMove.Start();
				return currentMove;
			}
			return null;
		}
		public function Hurt()
		{
			if (currentFrame == FRAME_HURT)
			{
				return;
			}
			gotoAndStop(FRAME_HURT);
			nWaitingFrame = 2;
		}
		public function Jump(upV:Number)
		{
			if (currentFrame == FRAME_LAND ||
			   currentFrame == FRAME_JUMP || currentState != STATE_FREE)
			{
				return;
			}
			currentState = STATE_FREE;
			gotoAndStop(FRAME_JUMP);
			physicalCore.velocityUp = upV;
		}
		public function Run(dir:int)
		{			
			//Move(-dir * this.speed);
			
			if (currentState != STATE_FREE)
			{
				return;
			}
			scaleX = Math.abs(scaleX) * dir;
			if (currentFrame == FRAME_RUN || !physicalCore.IsOverground())
			{
				return;
			}
			currentState = STATE_FREE;
			gotoAndStop(FRAME_RUN);
		}
		
		public function Move(dir:int)
		{
			if (currentState == STATE_FREE)
			{
				physicalCore.velocityX +=  -dir * this.speed;
				if (physicalCore.velocityX <  -  MAX_SPEED)
				{
					physicalCore.velocityX =  -  MAX_SPEED;
				}
				else if (physicalCore.velocityX > MAX_SPEED)
				{	
					physicalCore.velocityX = MAX_SPEED;
				}
			}
		}
	}

}